<?php

class MsgLogController extends MemberBaseController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/member';


	/**
	 * Deletes a particular model.
	 * If deletion is successful, the browser will be redirected to the 'admin' page.
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}

	/**
	 *逻辑删除
	 * @param integer $id the ID of the model to be deleted
	 */
	public function actionLogicDelete($id)
	{
		MsgLog::model()->LogicDelete($id);
		$this->redirect('/member/msglog/remind');
	}

	/**
	 * 交易提醒
	 */
	public function actionAdmin()
	{
		//数据测试
		//OrdMsg::model()->setOrdMsg('43',rand(40358243476,40454463476),rand(1,7));
		$model=new MsgLog('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['MsgLog']))
			$model->attributes=$_GET['MsgLog'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}

	/**
	 * 删除交易提醒
	 */
	public function actionDeleteOrdMsg()
	{
		$ids = Yii::app()->request->getParam('ids',0);
		$idbox = array(0);
		if(is_array($ids) && count($ids)){
			foreach($ids as $key=>$id){
				(int)$id && $idbox[] = (int)$id;
			}
		}elseif($ids){
			(int)$ids && $idbox[] = (int)$ids;
		}else{
			$message = '删除失败，未选中任何选项。';
		}
		if(count($idbox)>1 && OrdMsg::model()->deleteMemMsg($idbox, Yii::app()->user->userid)){
			$message = '删除成功。';
		}elseif(count($idbox)>1){
			$message = '删除失败。';
		}
		echo '<script>if(confirm("'.$message.'")){self.history.back();}</script>';
	}

	/**
	 * 删除会员信息
	 */
	public function actionDeleteMsg()
	{
		$ids = Yii::app()->request->getParam('ids',0);
		$idbox = array(0);
		if(is_array($ids) && count($ids)){
			foreach($ids as $key=>$id){
				(int)$id && $idbox[] = (int)$id;
			}
		}elseif($ids){
			(int)$ids && $idbox[] = (int)$ids;
		}else{
			$message = '删除失败，未选中任何选项。';
		}
		if(count($idbox)>1 && MsgLog::model()->deleteMsg($idbox, Yii::app()->user->userid)){
			$message = '删除成功。';
		}elseif(count($idbox)>1){
			$message = '删除失败。';
		}
		echo '<script>if(confirm("'.$message.'")){self.history.back();}</script>';
	}
	
	/**
	 * 消息提醒
	 */
	public function actionRemind()
	{
		//插入数据测试
		//MsgLog::model()->sendMemberInfo('160','特价168元2012新款夏休闲翻领特价168元2012新款夏休闲翻领特价168元012新款夏休闲翻领特价168元新 款夏休闲翻');
		$model=new MsgLog('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['MsgLog']))
			$model->attributes=$_GET['MsgLog'];
	
		$this->render('remind',array(
				'model'=>$model,
		));
	}
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=MsgLog::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='msg-log-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}


}
